﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.SharePoint;
using System.Web.UI.WebControls;
using System.Data;
using Microsoft.SharePoint.WebControls;

namespace TrafficTechFormsHistory
{
    class SPHelper
    {
        public string caml { get; set; }
        public int Allcnt { get; set; }

        public int InPrsCnt { get; set; }
        public int AppCnt { get; set; }
        public int RejCnt { get; set; }
        public string loginUser { get; set; }
        public int SubmitedCnt { get; set; }
        public int failedOnStartCnt { get; set; }

        DataTable dtResult;

        int cnt;

        public void CreateTable()
        {
            dtResult = CreateDataColumns("Department","AllForms", "Submitted", "Inprogress", "Approved", "Rejected","WebName","FailedToStart","Lnk");
        }

        public void GetListItems(string lstName,Label lblError,string fldRefName,string WebName)
        {
            try
            {
                Allcnt = GetAllItems(lstName,WebName);

                SubmitedCnt=SubmitedItemsCnt(lstName,WebName);
    
                if (lstName == "Purchase Requisition Form")
                    GetItemsWfStatus_spec("RFQAMApproved", fldRefName);
                else
                GetItemsWfStatus("16", fldRefName);
                AppCnt = GetItems(lstName, caml, WebName);

                string b = CheckNull(AppCnt);

                if (lstName == "Purchase Requisition Form")
                    GetItemsWfStatus_spec("Rejected", fldRefName);
                else
                    GetItemsWfStatus("17", fldRefName);
                RejCnt = GetItems(lstName, caml, WebName);

                string c = CheckNull(RejCnt);
                string lnk = string.Empty;
                if (lstName == "Purchase Requisition Form")
                {
                    InPrsCnt = SubmitedCnt - AppCnt - RejCnt;
                    lnk = string.Format("/{0}/{1}/Forms/Submitted By.aspx", WebName, lstName);
                }
                else
                {
                    GetItemsWfStatusInp(fldRefName);
                    InPrsCnt = GetItems(lstName, caml, WebName);
                    lnk = string.Format("/{0}/{1}/Forms/SubmittedForms.aspx", WebName, lstName);
                }

                GetItemsWfStatus("6", fldRefName);
                failedOnStartCnt = GetItems(lstName, caml, WebName);

                string a = CheckNull(InPrsCnt);
                string d=CheckNull(failedOnStartCnt);

                dtResult.Rows.Add(lstName, Allcnt,SubmitedCnt, a, b, c, WebName,d,lnk);
                               

               // dtResult.Rows.Add(lstName, SubmitCnt, InPrsCnt, AppCnt, RejCnt, WebName);
            }
            catch (Exception ee)
            {
               // throw;
                lblError.Text = lstName + ":" +  ee.Message;
            }
        }

        private int SubmitedItemsCnt(string lstName, string WebName)
        {
            
            SPQuery spq = new SPQuery();
            spq.Query = "<Where><Eq><FieldRef Name=\"Author\" /><Value Type=\"User\">"+loginUser+"</Value></Eq></Where>";
            int SubCnt=0;
            using (SPSite oSite = new SPSite(SPContext.Current.Site.Url))
            {
                using (SPWeb oWeb = oSite.OpenWeb(WebName))
                {
                    SubCnt = oWeb.Lists.TryGetList(lstName).GetItems(spq).Count;
                }
            }
            return SubCnt;
        }

        protected string CheckNull(int objGrid)
        {
            if (object.Equals(objGrid, 0))
            {
                return "-";
            }
            else
            {
                return objGrid.ToString();
            }
        }  


        public void BindGrid(GridView gv)
        {
            gv.DataSource = dtResult;
            gv.DataBind();

        }

        void lnkBtnInp_Click(object sender, EventArgs e)
        {
            //throw new NotImplementedException();
        }
        public void GetItemsWfStatus_spec(string wfStatus, string fldRefName)
        {
            caml = "<Where><And><Eq><FieldRef Name=\"" + fldRefName + "\" /><Value Type=\"Text\">" + wfStatus + "</Value></Eq><Contains><FieldRef Name=\"Author\" />" +
            "<Value Type=\"User\">" + loginUser + "</Value></Contains></And></Where>";
        }

        public void GetItemsWfStatus(string wfStatus,string fldRefName)
        {
            //caml = "<Where><Eq><FieldRef Name='" + fldRefName + "' /><Value Type='WorkflowStatus'>" + wfStatus + "</Value></Eq></Where>";

            caml = "<Where><And><Eq><FieldRef Name='" + fldRefName + "' /><Value Type='WorkflowStatus'>" + wfStatus + 
                "</Value></Eq><Eq><FieldRef Name='Author' /><Value Type='User'>" + loginUser +"</Value></Eq></And></Where>";
        }

        public void GetItemsWfStatusInp(string fldRefName)
        {
            //caml = "<Where><And><Neq><FieldRef Name='" + fldRefName + "' /><Value Type='WorkflowStatus'>16</Value></Neq><Neq><FieldRef Name='" + fldRefName + "' /><Value Type='WorkflowStatus'>17</Value></Neq></And></Where>";

            caml = "<Where><And><Neq><FieldRef Name='" + fldRefName + "' /><Value Type='WorkflowStatus'>16</Value></Neq><And><Neq><FieldRef Name='" 
                + fldRefName + "' /><Value Type='WorkflowStatus'>17</Value></Neq><Eq><FieldRef Name='Author' /><Value Type='User'>" + loginUser + 
                "</Value></Eq></And></And></Where>";


        }


        public int GetAllItems(string LstTitle, string WebName)
        {
            SPSecurity.RunWithElevatedPrivileges(delegate()
            {
                using (SPSite oSite = new SPSite(SPContext.Current.Site.Url))
                {
                    using (SPWeb oWeb = oSite.OpenWeb(WebName))
                    {
                        SPList oLst = oWeb.Lists.TryGetList(LstTitle);
                        SPListItemCollection oLstItmColl;
                        if (oLst != null)
                        {
                            oLstItmColl = oLst.Items;
                            cnt = oLstItmColl.Count;
                        }
                    }
                }
            });
            return cnt;
        }

        public int GetItems(string LstTitle, string camlQry, string WebName)
        {
            SPQuery qry = new SPQuery();
            qry.Query = camlQry;
            
            using(SPSite oSite = new SPSite(SPContext.Current.Site.Url))
            {
              using (SPWeb oWeb = oSite.OpenWeb(WebName))
              {
                
                SPList oLst = oWeb.Lists.TryGetList(LstTitle);
                SPListItemCollection oLstItmColl = oLst.GetItems(qry);
                cnt = oLstItmColl.Count;
              }
            }
            return cnt;
        }

        public static DataTable CreateDataColumns(params string[] colna)
        {
            DataTable dt = new DataTable();
            foreach (string item in colna)
            {
                dt.Columns.Add(item);
            }
            return dt;
        }

        public void GetLoginUser()
        {
            SPContext currentContext = SPContext.Current;
            loginUser = string.Empty;

            if (currentContext != null && currentContext.Web.CurrentUser != null)
            {
                SPWeb web = SPContext.Current.Web;
                loginUser = web.CurrentUser.Name;
            }
            else
            {
                loginUser = System.Web.HttpContext.Current.User.Identity.Name;
            }
        }


        
    }
}
